package com.xuetang9.fujian.view;

import com.xuetang9.fujian.model.Group;
import com.xuetang9.fujian.model.User;
import com.xuetang9.fujian.net.client.Client;
import com.xuetang9.fujian.view.panel.RelationListPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;
import java.util.List;

/**
 * @author: fujian
 * @description: 聊天系统的主界面
 * @date: 2021/6/8 10:51
 * @version: V1.0
 * @package: com.xuetang9.fujian.view
 **/
@SuppressWarnings("all")
public class MainFrame extends JFrame {
    private JPanel contentPane = null;
    private JPanel personPanel = new JPanel();
    private JLabel lblHead = new JLabel();
    private JLabel lblSignature = new JLabel("self signature");
    private JLabel lblNickName = new JLabel("nick name");
    private JTextField txtSearch = new JTextField();
    
    private JPanel mainPanel = new JPanel();
    private JScrollPane relationListScrollPane = new JScrollPane();
    private RelationListPanel relationListPanel = new RelationListPanel();
    public static Client client;
    private User loginUser;
    private Map<Group, List<User>> groupsPersons;
    public MainFrame(){
        init();
        setVisible(true);
    }

    public MainFrame(User loginUser){
        this.loginUser = loginUser;
        init();
        setVisible(true);
    }

    private void load(){
        lblNickName.setText(loginUser.getNickName());
        lblSignature.setText(loginUser.getSignature());
        remoteLoadRelation();
    }

    public void remoteLoadRelation() {
        HashMap<String, Object> message = new HashMap<>();
        message.put("account",loginUser.getAccount());
        message.put("handlerClass","com.xuetang9.fujian.net.request.LoadRelationRequestHandler");
        client.sendMessage(message);
    }

    public void loadRelationPersons(Map<Group, List<String >> groups,List<User> relationPersons){
        loginUser.setGroups((HashMap<Group, List<String>>) groups);
        groupsPersons = new LinkedHashMap<>();
        Set<Map.Entry<Group, List<String>>> entries = loginUser.getGroups().entrySet();
        for (Map.Entry<Group, List<String>> group : entries) {
            List<User> groupPersons = new ArrayList<>();
            List<String> accounts = group.getValue();
            for (User person : relationPersons) {
                if (accounts.contains(person.getAccount())){
                    groupPersons.add(person);
                }
            }
            groupsPersons.put(group.getKey(),groupPersons);
        }
        relationListPanel.loadGroups(groupsPersons);
    }

    private void init() {
        setTitle("main frame");
        setSize(300,700);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(new BorderLayout());
        // config components
        configPersonPanel();
        configMainPanel();
        configRelationListScrollPanel();
        // install components
        installComponents();
        // install listeners
        installListeners();
        
    }

    private void configPersonPanel() {
        // config personal panel
        configHeadLabel();
        configNickName();
        configSignatureLabel();
        personPanel.setPreferredSize(new Dimension(0,70));
        personPanel.setOpaque(false);
        personPanel.setLayout(null);
        personPanel.setBackground(Color.YELLOW);
    }

    private void configHeadLabel() {
        lblHead.setBounds(20,5,55,55);
        lblHead.setBackground(Color.RED);
        lblHead.setOpaque(true);
    }

    private void configNickName() {
        lblNickName.setBounds(85,5,100,30);
        lblNickName.setForeground(Color.BLACK);
    }

    private void configSignatureLabel() {
        lblSignature.setBounds(85,35,100,15);
    }

    private void configMainPanel() {
        // configure search field
        configSearchField();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBackground(Color.PINK);
    }

    private void configSearchField() {
        txtSearch.setPreferredSize(new Dimension(0,30));
    }

    private void configRelationListScrollPanel() {
        relationListScrollPane.setViewportView(relationListPanel);
        relationListScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        relationListScrollPane.getViewport().setBackground(Color.WHITE);
    }

    /**
     * @desc install all self components
     * @param null
     * @return
     * @author fujian
     * @date 2021/6/9 09:05:08
     * @version 1.0
     **/
    private void installComponents() {
        personPanel.add(lblHead);
        personPanel.add(lblNickName);
        personPanel.add(lblSignature);
        mainPanel.add(txtSearch,BorderLayout.NORTH);
        mainPanel.add(relationListScrollPane,BorderLayout.CENTER);
        contentPane.add(personPanel,BorderLayout.NORTH);
        contentPane.add(mainPanel,BorderLayout.CENTER);
    }
    /**
     * @desc install all components events
     * @param null
     * @return  
     * @author fujian
     * @date 2021/6/9 09:25:05
     * @version 1.0
     **/
    private void installListeners() {
        addWindowListener(new WindowCloseHandler());
    }


    public User getLoginUser() {
        return loginUser;
    }

    public void setLoginUser(User loginUser) {
        this.loginUser = loginUser;
    }

    private class WindowCloseHandler extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e) {
            HashMap<String, Object> message = new HashMap<>();
            message.put("closeFrame","MainFrame");
            message.put("exit",true);
            message.put("handlerClass","com.xuetang9.fujian.net.request.ClientExitRequestHandler");
            MainFrame.client.sendMessage(message);
        }
    }

    public static void main(String[] args) {
        new MainFrame();
    }


}
